package cm.mefobe.android.telecomcost.view;

import java.util.ArrayList;

import cm.mefobe.android.telecomcost.Utility;
import cm.mefobe.android.telecomcost.dialog.DaysPickerDialog;
import cm.mefobe.android.telecomcost.entities.Day;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;

public class DaysPickerView extends TableLayout {

	private Context _context;	
	private EditText _txtDays;
	private Button _btnSetTime;
	private DaysPickerDialog dialog;	
	private TableRow row;
	private ArrayList<Day> selectedDays;

	public DaysPickerView(Context context) {
		super(context);
		Log.i("DaysPickerView", "creating the days picker view");
		_context = context;		
	}	

	private void setDaysPickerView() {
		if (isInEditMode()) {
			Log.i("DaysPickerView", "isInEditMode");
		} else {
			if (row == null) {				
				Log.i("DaysPickerView", "adding views...");
				row = new TableRow(_context);
				
				_txtDays = new EditText(_context);
				_btnSetTime = new Button(_context);								
				_txtDays.setEnabled(false);	
				_txtDays.setTextSize(14);
				_txtDays.setText("select the days of week");

				_btnSetTime.setText("Pick");
				_btnSetTime.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						showDaysPickerDialog();
					}
				});
				
				_txtDays.setOnClickListener(new OnClickListener() {

					public void onClick(View v) {
						showDaysPickerDialog();
					}
				});
				
				row.addView(_txtDays);
				row.addView(_btnSetTime,LayoutParams.FILL_PARENT);

				addView(row);
				setColumnStretchable(0,true);
			} else {
				Log.i("DaysPickerView", "DaysPickerView already set");
			}
		}
	}

	public DaysPickerView(Context context, AttributeSet attrs) {
		super(context, attrs);
		Log.i("DaysPickerView",
				"creating the time picker view with AttributeSet");

		_context = context;
	}

	protected void showDaysPickerDialog() {
		dialog = new DaysPickerDialog(_context,this);
		dialog.show();
	}	

	@Override
	protected void onLayout(boolean changed, int l, int t, int r, int b) {
		Log.i("DaysPickerView", "onLayout");
		super.onLayout(changed, l, t, r, b);
	}

	@Override
	protected void onAttachedToWindow() {
		Log.i("DaysPickerView", "onAttachedToWindow");
		super.onAttachedToWindow();
	}

	@Override
	protected void onDraw(Canvas canvas) {
		Log.i("DaysPickerView", "onDraw");
		super.onDraw(canvas);
	}

	@Override
	protected void onFinishInflate() {
		Log.i("DaysPickerView", "onFinishInflate");
		super.onFinishInflate();
		setDaysPickerView();
	}
	
	public boolean daysOfWeekSet()
	{
		return selectedDays != null;
	}

	@Override
	public void updateViewLayout(View view,
			android.view.ViewGroup.LayoutParams params) {
		Log.i("DaysPickerView", "updateViewLayout");
		super.updateViewLayout(view, params);
	}

	public void setSelectedDays(ArrayList<Day> selectedDays) {
		this.selectedDays = selectedDays;
		_txtDays.setText(Utility.formatDays(selectedDays));
	}

	public ArrayList<Day> getSelectedDays() {
		return selectedDays;
	}
}
